/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.model;

import java.util.ArrayList;
import java.util.List;

import sw6.digipecs.database.DBAdapter;
import sw6.digipecs.database.IDCursor;
import sw6.digipecs.database.SituationCursor;
import sw6.digipecs.exceptions.DatabaseException;
import sw6.digipecs.exceptions.IDNotFoundException;
import sw6.digipecs.exceptions.ImageIsEmptyException;
import sw6.digipecs.exceptions.ImageIsInvalidException;
import android.content.Context;

public class Situation {
	
	private DBAdapter mDB;
	private String mName;
	private int mId;
	private boolean mActive;
	private boolean mValid = true;

	public Situation(Context context, int id) throws IDNotFoundException {
		SituationCursor cursor;
		
		mDB = new DBAdapter(context);
		
		synchronized (mDB) {
			mDB.open();
			cursor = mDB.fetchSituation(id);

			if (cursor.moveToFirst()) {
				mName = cursor.getName();
				mId = id;
				mActive = cursor.getActive();
			} else {
				throw new IDNotFoundException();
			}

			cursor.close();
			mDB.close();
		}
	}
	
	public static Situation CreateNew(Context context, String name) throws DatabaseException {
		long id;
		Situation situation;
		
		DBAdapter db = new DBAdapter(context);
		db.open();
		id = db.addSituation(name);
		db.close();
		
		try {
			situation = new Situation(context, (int)id);
		} catch (IDNotFoundException e) {
			throw new DatabaseException();
		}
		
		return situation;
	}
	
	public static List<Situation> getAll(Context context) {
		List<Situation> situations = new ArrayList<Situation>();
		SituationCursor cursor;
		
		DBAdapter db = new DBAdapter(context);
		db.open();
		cursor = db.fetchSituations();
		if (cursor.moveToFirst()) {
			do {
				try {
					situations.add(new Situation(context, cursor.getId()));
				} catch (IDNotFoundException e) { }
			} while (cursor.moveToNext());
		}
		cursor.close(); 
		db.close();
		
		return situations;
	}
	
	public static void setActive(Context context, Situation situation) {
		DBAdapter db = new DBAdapter(context);
		db.open();
		db.setSituationActive(situation.getId());
		db.close();
	}
	
	public String getName() {
		if (!mValid) throw new ImageIsInvalidException();
			
		return mName;
	}
	
	public int getId() {
		if (!mValid) throw new ImageIsInvalidException();
		
		return mId;
	}
	
	public boolean isActive() {
		if (!mValid) throw new ImageIsInvalidException();
		
		return mActive;
	}
	
	/**
	 * \remark PLEASE DO NOT USE THE OBJECT AFTER RUNNING THIS METHOD
	 */
	public void delete() {
		if (!mValid) throw new ImageIsInvalidException();
		
		mValid = false;
		synchronized (mDB) {
			mDB.open();
			mDB.deleteSituation(mId);
			mDB.close();
		}
	}

	public static int getActiveSituation(Context context) throws DatabaseException {
		DBAdapter adapter = new DBAdapter(context);
		adapter.open();
		int id = adapter.getSituationActive();
		adapter.close();
		
		return id;
	}
	
	public ArrayList<ExtImageInSituation> getAllPictures(Context c)
	{
		if (!mValid) throw new ImageIsInvalidException();
		
		ArrayList<ExtImageInSituation>imagesToReturn = new ArrayList<ExtImageInSituation>();
		synchronized (mDB) {
			mDB.open(); 
			IDCursor imgCursor = mDB.fetchAllPicturesForSituation(mId); 
			if(imgCursor.moveToFirst())
			{
				do {
					try {
						ExtImageInSituation image = new ExtImageInSituation(c, imgCursor.getPictureID(), mId);
						imagesToReturn.add(image); 
					} catch (IDNotFoundException e) {
						e.printStackTrace();
					} 
				} while (imgCursor.moveToNext());
			}
			imgCursor.close(); 
			mDB.close();
		}
		return imagesToReturn; 
	}
	
	public void addPicture(ExtImage image) throws DatabaseException, ImageIsEmptyException {
		if (!mValid) throw new ImageIsInvalidException();
		
		synchronized (mDB) {
			mDB.open();
			mDB.addPictureToSituation(image.getId(), mId);
			mDB.close();
		}
	}
	public void deletePicture(ExtImage image) throws ImageIsEmptyException
	{
		if (!mValid) throw new ImageIsInvalidException();
		
		synchronized (mDB) {
			mDB.open();
			mDB.removePictureFromSituation(image.getId(), mId);
			mDB.close();
		}
	}
	
	public void removePicture(ExtImage image) throws ImageIsEmptyException {
		if (!mValid) throw new ImageIsInvalidException();
		
		synchronized (mDB) {
			mDB.open();
			mDB.removePictureFromSituation(image.getId(), mId);
			mDB.close();
		}
	}
	public void updateNameInDb(Context context, String name) {
		DBAdapter adapter = new DBAdapter(context); 
		adapter.open(); 
		adapter.setSituationName(mId, name);
		adapter.close(); 
	}
}
